package com.zzg.srb.core.controller.api;


import com.alibaba.fastjson.JSON;
import com.zzg.srb.common.constant.Constant;
import com.zzg.srb.common.exception.Assert;
import com.zzg.srb.common.result.R;
import com.zzg.srb.common.result.ResponseEnum;
import com.zzg.srb.core.hfb.RequestHelper;
import com.zzg.srb.core.service.UserAccountService;
import com.zzg.srb.core.utils.CurrentLoginUserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 用户账户 前端控制器
 * </p>
 *
 * @author zhengzg
 * @since 2022-05-27
 */
@Api(tags = "会员账户")
@RestController
@RequestMapping("/api/core/userAccount")
public class UserAccountController {
    Logger log = LoggerFactory.getLogger(UserAccountController.class);

    @Resource
    private UserAccountService userAccountService;

    @ApiOperation("充值")
    @PostMapping("/auth/commitCharge/{chargeAmt}")
    @ApiImplicitParam(value = "充值金额",required = true)
    public R commitCharge(@PathVariable BigDecimal chargeAmt) {
        // 获取用户ID
        Long userId = CurrentLoginUserUtils.getLoginUser();
        Assert.notNull(userId, ResponseEnum.WEIXIN_FETCH_USERINFO_ERROR);
        String formStr = userAccountService.commitCharge(chargeAmt, userId);
        return R.ok().data(formStr);
    }

    @ApiOperation("用户充值异步回调")
    @PostMapping("/notify")
    public String notifyMethods(HttpServletRequest request) {
        Map<String, Object> paremMap = RequestHelper.switchMap(request.getParameterMap());
        log.info("用户充值异步回调：" + JSON.toJSONString(paremMap));

        // 检验签名
        if (RequestHelper.isSignEquals(paremMap)) {
            // 充值成功交易
            if (Objects.equals(Constant.RECHARGE,Constant.RESULT_CODE)) {
                return userAccountService.notifyMethods(paremMap);
            } else {
                log.info("用户充值异步回调失败：" + JSON.toJSONString(paremMap));
                return Constant.SUCCESS;
            }
        } else {
            log.info("用户充值异步回调错误：" + JSON.toJSONString(paremMap));
            return Constant.FAIL;
        }
    }

    @ApiOperation("查询账户余额")
    @GetMapping("/auth/getAccount")
    public R getAccount() {
        Long userId = CurrentLoginUserUtils.getLoginUser();
        return R.ok().data(userAccountService.getAccount(userId));
    }

    @ApiOperation("用户提现")
    @PostMapping("/auth/commitWithdraw/{fetchAmt}")
    public R commitWithdraw(
            @ApiParam(value = "金额", required = true)
            @PathVariable BigDecimal fetchAmt, HttpServletRequest request) {

        Long userId = CurrentLoginUserUtils.getLoginUser();
        String formStr = userAccountService.commitWithdraw(fetchAmt, userId);
        return R.ok().data(formStr);
    }

    @ApiOperation("用户提现异步回调")
    @PostMapping("/notifyWithdraw")
    public String notifyWithdraw(HttpServletRequest request) {
        Map<String, Object> paramMap = RequestHelper.switchMap(request.getParameterMap());
        log.info("提现异步回调：" + JSON.toJSONString(paramMap));

        //校验签名
        if(RequestHelper.isSignEquals(paramMap)) {
            //提现成功交易
            if("0001".equals(paramMap.get("resultCode"))) {
                userAccountService.notifyWithdraw(paramMap);
            } else {
                log.info("提现异步回调充值失败：" + JSON.toJSONString(paramMap));
                return "fail";
            }
        } else {
            log.info("提现异步回调签名错误：" + JSON.toJSONString(paramMap));
            return "fail";
        }
        return "success";
    }

}

